package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.model.SkuInfo;
import com.atguigu.gmall.product.service.SkuManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * title:
 * author: Gyh
 * date:2023-04-19 18:15
 * Description:
 */
@RestController
@RequestMapping("/admin/product")
public class SkuManageController {

    @Autowired
    private SkuManageService skuManageService;

    /**
     * 保存SkuInfo,SKU信息
     * @param skuInfo
     * @return
     */
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuManageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * sku分页列表
     * @param page
     * @param limit
     * @param category3Id
     * @return
     */
    @GetMapping("/list/{page}/{limit}")
    public Result getSkuListByPage(@PathVariable("page") Long page,
                                   @PathVariable("limit") Long limit,
                                   @RequestParam("category3Id") Long category3Id){
        //创建分页对象
        Page<SkuInfo> skuInfoPage = new Page<>(page,limit);
        //调用业务层
        skuInfoPage = skuManageService.getSkuListByPage(skuInfoPage,category3Id);
        return Result.ok(skuInfoPage);
    }

    /**
     * 商品SKU上架
     * @param skuId
     * @return
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable("skuId") Long skuId){
        skuManageService.onSale(skuId);
        return Result.ok();
    }

    /**
     * 商品SKU下架
     * @return
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable("skuId") Long skuId){
        skuManageService.cancelSale(skuId);
        return Result.ok();
    }

}
